/**
 * 作者:李杰
 * email: maillijie@gmail.com
 */
package com.javainfo.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.javainfo.dao.inf.ArticleCategoryDaoIf;
import com.javainfo.dao.inf.ArticleDaoIf;
import com.javainfo.dao.inf.AttachmentsDaoIf;
import com.javainfo.dao.inf.BaseDaoIf;
import com.javainfo.dao.inf.UserDaoIf;
import com.javainfo.entity.Article;
import com.javainfo.entity.ArticleCategory;
import com.javainfo.entity.User;
import com.javainfo.service.inf.ArticleServiceIf;
import com.javainfo.simpledto.ArticleDto;
import com.javainfo.util.Const;
import com.javainfo.util.FileUploadSupport;
import com.javainfo.util.UploadFileManageUtil;

@SuppressWarnings("unchecked")
public class ArticleService extends BaseService<Article> implements ArticleServiceIf{

	/**  articleDao */
	private ArticleDaoIf articleDao;

	/** attachmentsDao */
	private AttachmentsDaoIf attachmentsDao;
	
	/** articleCategoryDao */
	private ArticleCategoryDaoIf articleCategoryDao;
	
	/** userDao */
	private UserDaoIf userDao;
	
	@Override
	public BaseDaoIf<Article> getBaseDao() {
		return articleDao;
	}

	public ArticleDaoIf getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDaoIf articleDao) {
		this.articleDao = articleDao;
	}

	public ArticleCategoryDaoIf getArticleCategoryDao() {
		return articleCategoryDao;
	}

	public void setArticleCategoryDao(ArticleCategoryDaoIf articleCategoryDao) {
		this.articleCategoryDao = articleCategoryDao;
	}

	public AttachmentsDaoIf getAttachmentsDao() {
		return attachmentsDao;
	}

	public void setAttachmentsDao(AttachmentsDaoIf attachmentsDao) {
		this.attachmentsDao = attachmentsDao;
	}
	
	public void updateClickNo(Map session, Integer id) {
		Article _article = articleDao.getById(id);
		Object object = session.get(Const.ARTICLE_IDS);
		if(object==null){
			object = new HashSet<Integer>();
			session.put(Const.ARTICLE_IDS, object);
			Set<Integer> articleIds = (Set<Integer>)object;
			articleIds.add(_article.getId());
			this.addClickNo(_article);
		}else{
			Set<Integer> articleIds = (Set<Integer>)session.get(Const.ARTICLE_IDS);
			if(!articleIds.contains(_article.getId())){
				articleIds.add(_article.getId());
				this.addClickNo(_article);
			}
		}
		articleDao.update(_article);
	}

	private void addClickNo(Article article) {
		if (article.getClickNo() == null) {
			article.setClickNo(Integer.valueOf(1));
		} else {
			article.setClickNo(article.getClickNo() + 1);
		}
	}
	
	@Override
	public void update(Article obj) {
		Article article = articleDao.getById(obj.getId());
		article.setCategory(obj.getCategory());
		article.setUser(obj.getUser());
		article.setContent(obj.getContent());
		article.setTitle(obj.getTitle());
		articleDao.update(article);
	}
	
	@Override
	public void add(Article obj) {
		if(obj.getAttachmentsSet()!=null){
			attachmentsDao.addAll(obj.getAttachmentsSet());
		}
		super.add(obj);
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.ArticleServiceIf#saveNewArticle(com.javainfo.entity.User, com.javainfo.simpledto.ArticleDto, com.javainfo.util.FileUploadSupport)
	 */
	public Integer saveNewArticle(User user, ArticleDto articleDto,
			FileUploadSupport files) {
		Article article = new Article();
		article.setTitle(articleDto.getTitle());
		Integer categoryId = articleDto.getCategoryId() == null ? null
				: Integer.valueOf(articleDto.getCategoryId());
		Integer subCategoryId = articleDto.getSubCategoryId() == null ? null
				: Integer.valueOf(articleDto.getSubCategoryId());
		article.setCategory(articleCategoryDao.getById(categoryId==null?-1:categoryId));
		article.setSubCategory(articleCategoryDao.getById(subCategoryId==null?-1:subCategoryId));
		article.setContent(articleDto.getContent());
		article.setUser(user);
		if (Const.UploadFileManage.TO_DB.equals(files.getFileSaveType())) {
			UploadFileManageUtil.saveUploadFileToDB(files,(User) user, article);
		} else if (Const.UploadFileManage.TO_FILE_SYSTEM
				.equals(files.getFileSaveType())) {
			UploadFileManageUtil.copyUploadFiles(files,(User) user, article);
		}
		add(article);
		return article.getId();
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.ArticleServiceIf#getArticleInfo(java.lang.Integer)
	 */
	public ArticleDto getArticleInfo(Integer id) {
		Article article = getById(id);
		return getConvertUtil().convertArticleToSimpleDto(article);
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.ArticleServiceIf#updateArticle(com.javainfo.entity.User, com.javainfo.simpledto.ArticleDto)
	 */
	public void updateArticle(User user, ArticleDto articleDto) {
		if(articleDto!=null){
			int categoryId = articleDto.getCategoryId()==null?-1:Integer.valueOf(articleDto.getCategoryId());
			int subCategoryId = articleDto.getSubCategoryId()==null?-1:Integer.valueOf(articleDto.getSubCategoryId());
			ArticleCategory articleCategory = articleCategoryDao.getById(categoryId);
			ArticleCategory subArticleCategory = articleCategoryDao.getById(subCategoryId);
			Article article = getById(articleDto.getId()==null?-1:articleDto.getId());
			if (article!=null) {
				article.setCategory(articleCategory);
				article.setSubCategory(subArticleCategory);
				article.setUser(user);
				article.setUpdateTime(new Date());
				article.setTitle(articleDto.getTitle());
				article.setContent(articleDto.getContent());
				update(article);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.javainfo.service.inf.ArticleServiceIf#getArticlesByUserAccount(java.lang.String)
	 */
	public List<ArticleDto> getArticlesByUserAccount(String userAccount) {
		List<Article> list = articleDao.serachArticleByUserAccount(userAccount);
		List<ArticleDto> articleDtoList = new ArrayList<ArticleDto>();
		for(Article _article : list){
			articleDtoList.add(getConvertUtil().convertArticleToSimpleDto(_article));
		}
		return articleDtoList;
	}

	/**
	 * @return the userDao
	 */
	public UserDaoIf getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDaoIf userDao) {
		this.userDao = userDao;
	}

}
